package fenetres;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import basedonnee.DBCon;
import classe.ConnexPlat;
public class Plat extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	ConnexPlat oplat;
	JPanel pane, imgPanel;
	JLabel restNomLbl;
	JLabel platNomLbl;
	JLabel catLbl;
	JLabel prixLbl;
	JLabel descLbl;
	JLabel label, label1;	
	JTextField restTxt;
	JTextField platTxt;
	JTextField catTxt;
	JTextField prixTxt;
	JTextPane descTxt;
	JScrollPane scroll = new JScrollPane( );
    JButton validerBut, annulerBut, fermerBut;
	
	public Plat(){
		
		initComponent();
		//recuperation de la taille de la fenetre
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
		this.setLocation((screen.width - this.getSize().width)/2,(screen.height - this.getSize().height)/2);
		this.setLocationRelativeTo(null);
		this.getContentPane().add(pane);
		this.getContentPane().add(scroll);
		this.setSize(550,500);
		this.setTitle("Ajouter Plat");
		this.setVisible(true);
		this.setBackground(Color.white);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);	
		this.setLocationRelativeTo(null);
		

	}
	public void initComponent() {
		pane = new JPanel();
		pane.setSize(550,500);
		pane.setLayout(null);
		
	     
		ImageIcon background = new ImageIcon("image/Plat.jpeg");
		JLabel label = new JLabel();
		label.setIcon(background);
//		ImageIcon background1 = new ImageIcon("image/Plat1.jpeg");
//		JLabel label1 = new JLabel();
//		label1.setIcon(background1);
		JLabel restNomLbl = new JLabel (" Nom Restaurant (*): ");
		restNomLbl.setForeground(Color.blue);
		restTxt = new JTextField ();
		
		JLabel platNomLbl = new JLabel(" Nom Plat (*): ");
		platNomLbl.setForeground(Color.blue);
		platTxt = new JTextField();
		
		JLabel catLbl = new JLabel(" Categorie Plat : ");
		catLbl.setForeground(Color.blue);
		catTxt = new JTextField();
		
		JLabel prixLbl = new JLabel(" Prix Plat : ");
		prixLbl.setForeground(Color.blue);
		prixTxt = new JTextField();
		
		JLabel descLbl = new JLabel(" Description Plat : ");
		descLbl.setForeground(Color.blue);
		descTxt = new JTextPane();
		
		scroll = new JScrollPane(descTxt);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		annulerBut = new JButton("Annuler");
		validerBut = new JButton("Valider");
		fermerBut = new JButton("Fermer");

		
		
		pane.add(restNomLbl);
		pane.add(restTxt);
		pane.add(platNomLbl);
		pane.add(platTxt);
		pane.add(catLbl);
		pane.add(catTxt);
		pane.add(prixLbl);
		pane.add(prixTxt);
		pane.add(descLbl);
		pane.add(descTxt);
		pane.add(validerBut);
		pane.add(fermerBut);
		pane.add(annulerBut);
		pane.add(scroll);
		pane.add(label);
		//pane.add(label1);
		
		//label1.setBounds(0,0,270,120);
		label.setBounds(195, 0,550,120);
		
		
		restNomLbl.setBounds(5,140,190,25);
		restTxt.setBounds(200,140,250,25);
		platNomLbl.setBounds(5,180,190,25);
		platTxt.setBounds(200,180,250,25);
		catLbl.setBounds(5,220,190,25);
		catTxt.setBounds(200,220,250,25);
		prixLbl.setBounds(5,260,190,25);
		prixTxt.setBounds(200,260,250,25);
		descLbl.setBounds(5,300,190,25);
		descTxt.setBounds(200,300,250,80);
		
		validerBut.setBounds(150,410,100,25);
		fermerBut.setBounds(270, 410, 100, 25);
		annulerBut.setBounds(390,410,100,25);
		
		
		validerBut.setText("Valider");
		validerBut.addActionListener(this);
		fermerBut.setText("Fermer");
		fermerBut.addActionListener((ActionListener) this);
		annulerBut.setText("Annuler");
		annulerBut.addActionListener( (ActionListener) this);
		
		
	}	

		
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == fermerBut){		      

			this.dispose();
			}
	
			if(e.getSource()== annulerBut){
				restTxt.setText("");
				platTxt.setText("");
				catTxt.setText("");
				prixTxt.setText("");
				descTxt.setText("");
				
			}
		    if (e.getSource() == validerBut) {
		    
		      validerBut_actionPerformed();
		      oplat = new ConnexPlat();
		      oplat.setPlat_lib(this.platTxt.getText());
		      oplat.setCateg_lib(this.catTxt.getText());
		      oplat.setPrix(this.platTxt.getText());
		      oplat.setDesc_lib(this.descTxt.getText());
		     
		      if (!oplat.getPlat_lib().isEmpty()){
		    	  
		    	 DBCon dbManager = new DBCon();
		    	 boolean bRes = oplat.storePlat(dbManager);
					if(bRes) {
						JOptionPane.showMessageDialog(null, "Ajout plat reussie !!!","Confirmation",JOptionPane.INFORMATION_MESSAGE);
						this.dispose();
					}
					else JOptionPane.showMessageDialog(null, "Ajout Produit echoue !!!","Erreur",JOptionPane.ERROR_MESSAGE);
					

				}
			}
			
		
			
		  }
	private void validerBut_actionPerformed() {
		// TODO Auto-generated method stub
		boolean etat = true;
		if((restTxt.getText().isEmpty()) || (platTxt.getText().isEmpty()))
		{
			etat = false;
			
			JOptionPane.showMessageDialog (null, "Les champs obligatoires ne sont pas remplis !", "Information", JOptionPane.WARNING_MESSAGE) ;
		}
		
		if((restTxt.getText().contains("\"")) || (restTxt.getText().contains("\'")) || (restTxt.getText().contains("\\")) || (restTxt.getText().contains("%")))
		{
				JOptionPane.showMessageDialog (null, "Les champs ne doivent pas contenir les caractères spéciaux !", "Information", JOptionPane.WARNING_MESSAGE) ;
				 etat = false;;
			}
		
		 if (etat) { //la saisie est juste
		        int result=JOptionPane.showConfirmDialog(null,"Voulez vous vraiment validerBut ce commentaire","Confirmation ajout",JOptionPane.YES_NO_OPTION);
		        if(result==0){//reponse oui
		        	
		        //Commentaire = new Commentaire1();
		        	//restTxt.setText("");
		        	//platTxt.setText("");
		  
		        }
	}
		 
	}
	
}



	
	
	